class Vocabulary(object):
    def __init__(self,token_to_idx=None):
        if token_to_idx is None:
            token_to_idx={}
        self._token_to_idx=token_to_idx
        self._idx_to_token={idx:token
                            for token,idx in self._token_to_idx.items()}

    def to_serializable(self):
        return {'token_to_idx':self._token_to_idx}

    @classmethod
    def from_serializable(cls,contents):
        return cls(**contents)

    def add_token(self,token):
        if token in self._token_to_idx:
            index=self._token_to_idx[token]
        else:
            index=len(self._token_to_idx)
            self._token_to_idx[token]=index
            self._idx_to_token[index]=token
        return index

    def add_many(self,tokens):
        return [self.add_token(token) for token in tokens]

    def lookup_token(self,token):
        return self._token_to_idx[token]

    def lookup_index(self,index):
        if index not in self._idx_to_token:
            raise KeyError("the index (%d) is not in the Vocabulary" % index)
        return self._idx_to_token[index]

    def __str__(self):
        return "<Vocabulary(size=%d)>" % len(self)

    def __len__(self):
        return len(self._token_to_idx)